package de.joergviola.mobj.client.database;

import java.util.Date;
import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;

import de.joergviola.mobj.shared.EntityReader;
import de.joergviola.mobj.shared.ObjectNotFoundException;
import de.joergviola.mobj.shared.Persistable;

public class CreateTable implements EntityReader {

	StringBuilder columns = new StringBuilder();

	private void column(String name, String type) {
		if (columns.length() > 0)
			columns.append(",");
		columns.append(name);
		columns.append(" ");
		columns.append(type);
	}

	@Override
	public String readString(String name) {
		column(name, "TEXT");
		return null;
	}

	@Override
	public Long readLong(String name) {
		column(name, "INTEGER");
		return null;
	}

	@Override
	public Double readDouble(String name) {
		column(name, "REAL");
		return null;
	}

	@Override
	public Date readDate(String name) {
		column(name, "DATETIME");
		return null;
	}

	@Override
	public List<Long> readListLong(String name) {
		column(name, "TEXT");
		return null;
	}

	@Override
	public List<String> readListString(String name) {
		column(name, "TEXT");
		return null;
	}

	@Override
	public String[] readStringArray(String name) {
		column(name, "TEXT");
		return null;
	}

	public String getSQL(String entityName) {
		StringBuilder sql = new StringBuilder();
		sql.append("CREATE TABLE IF NOT EXISTS ");
		sql.append(entityName);
		sql.append(" (");
		sql.append(columns.toString());
		sql.append(")");
		return sql.toString();
	}

	@Override
	public <T extends Persistable> void readPersistable(String name,
			Class<T> clazz, AsyncCallback<T> setter)
			throws ObjectNotFoundException {
		column(name, "INTEGER");
	}

}
